package part5assistclass;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 和上面的减法相反，这里是加法，好比集齐7个龙珠召唤神龙，或者人到齐了再开会
 * Thread-1收集了第2颗龙珠
 * Thread-5收集了第6颗龙珠
 * Thread-4收集了第5颗龙珠
 * Thread-3收集了第4颗龙珠
 * Thread-2收集了第3颗龙珠
 * Thread-0收集了第1颗龙珠
 * Thread-6收集了第7颗龙珠
 * 召唤神龙成功
 */
public class CyclicBarrierTest01 {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("召唤神龙成功");
        });

        for (int i = 1; i <= 7; i++) {
            int finalI = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "收集了第" + finalI + "颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}
